<?php

function needToGetSelectedTeams($hours) {
	return seeifNeedToCheck("updatedSelectedTeamsStatus", $hours);
}

function determineIfNeedSendReminders($hours) {
	return seeifNeedToCheck("remindersSent", $hours);
}

function seeifNeedToCheck($property, $hours) {
	$round = getCurrentRound();
	$roundDetails = getRoundProperty($property, $round);
	
	$now = strtotime("+" . $hours . " hours");
	$dateComponents = getdate($now);
	$needToCheck = false;
	$task = getTask($property);
	$timeToPerformTask = getTimeToPerformTask($task);
	
	if ($roundDetails->selectedTeamsReleaseDays == "BOTH") {
		if ($roundDetails->property == "NONE") {
			if (compareDateTimes($dateComponents, $timeToPerformTask)) {
				setRoundProperty($property, 'WEDNESDAY', $round);
				
				$needToCheck = true;
			}
		}
		else if ($roundDetails->property == "WEDNESDAY") {
			if (compareDateTimes($dateComponents, $timeToPerformTask)) {
				setRoundProperty($property, 'COMPLETE', $round);
				
				$needToCheck = true;
			}
		}
	}
	else if ($roundDetails->selectedTeamsReleaseDays == "THURSDAY" && $roundDetails->property == "NONE") {
		if (compareDateTimes($dateComponents, $timeToPerformTask)) {
			setRoundProperty($property, 'COMPLETE', $round);
			
			$needToCheck = true;
		}
	}
	
	return $needToCheck;
}

function compareDateTimes($dateComponents, $timeToPerformTask) {
	return $dateComponents["wday"] == $timeToPerformTask->day && $dateComponents["hours"] == $timeToPerformTask->hours && $dateComponents["minutes"] >= $timeToPerformTask->minutes;
}

function getTask($property) {
	if ($property == "remindersSent") {
		return TimeToPerformTaskType::SendReminders;
	}
	else {
		return TimeToPerformTaskType::UploadSelectedTeams;
	}
}

function finaliseRound() {
	$leagues = getActiveLeagues();
	$round = getCurrentRound();
	foreach ($leagues as $league) {
		$fixture = getRoundResults($league, $round);
		
		foreach ($fixture as $fixtureItem) {
			replacePlayers($round, $fixtureItem, "Home");
			replacePlayers($round, $fixtureItem, "Away");
		}
	}
	updateRoundTables();
	
	createLadder();
	createDisplayRound($round);
	createFixture();
}

function replacePlayers($round, $fixtureItem, $homeOrAway) {
	$playerScores = $fixtureItem->getZeroScoringPlayers("Home");
	if ($homeOrAway == "Away") {
		$playerScores = $fixtureItem->getZeroScoringPlayers("Away");
	}
	
	$replaced = 0;
	$fixtureItem->usedEmergencies = array();
	foreach ($playerScores as $playerScore) {
		if ($playerScore->score == 0 && $playerScore->positionInAFLTeam != null && $playerScore->positionInAFLTeam != "EMG" && !$playerScore->isInValid) {
			$highestScoringEmg = $fixtureItem->getHighestScoringEmergency($homeOrAway);
			$kpdEMG = $fixtureItem->getKPDEmergency($homeOrAway);
			$halfKpdEMG = $fixtureItem->getHalfKPDEmergency($homeOrAway);
			$kppEMG = $fixtureItem->getKPPEmergency($homeOrAway);
			$halfKppEMG = $fixtureItem->getHalfKPPEmergency($homeOrAway);
			$ruckEMG = $fixtureItem->getRuckEmergency($homeOrAway);
			$kppRuck = $fixtureItem->getKPPRuck($homeOrAway);
			$inDefence = isDefence($playerScore->playerPosition);
			$kppCount = $fixtureItem->getKppCount($homeOrAway);
						
			if ($playerScore->player->playerType == null || ($playerScore->player->playerType == PlayerType::Ruck && $playerScore->playerPosition != "R")) {
				if ($highestScoringEmg != null) {
					replacePlayer($fixtureItem, $playerScore, $highestScoringEmg, $round, ReplacementType::HSP, $replaced);
				}
			}
			else if ($playerScore->player->playerType == PlayerType::Ruck) {
				if ($fixtureItem->hasPlayingRuckOrSpareKPPRuck($homeOrAway)) {
					if ($highestScoringEmg != null) {
						replacePlayer($fixtureItem, $playerScore, $highestScoringEmg, $round, ReplacementType::HSP_With_Playing_Ruck, $replaced);
					}
				}
				else if ($ruckEMG != null) {
					replacePlayer($fixtureItem, $playerScore, $ruckEMG, $round, ReplacementType::Ruck_Emergency, $replaced);
				}
				else if ($kppRuck != null && $kppEMG != null) {
					$kppRuckInDefence = isDefence($kppRuck->playerPosition);
					if ($kppRuckInDefence && (($fixtureItem->getKppDefenderCount($homeOrAway) - 1) >= 2 || $kppEMG->isKPD)) {
						replacePlayer($fixtureItem, $playerScore, $kppEMG, $round, ReplacementType::KPP_With_KPP_Ruck_Slide_In_Defence, $replaced);
					}
					else if (!$kppRuckInDefence) {
						replacePlayer($fixtureItem, $playerScore, $kppEMG, $round, ReplacementType::KPP_With_KPP_Ruck_Slide, $replaced);
					}
				}
			}
			else if ($playerScore->player->playerType == PlayerType::KPP || $playerScore->player->playerType == PlayerType::Half_KPP) {
				if ($fixtureItem->kppRequirementsMet($homeOrAway)) {
					if ($highestScoringEmg != null) {
						replacePlayer($fixtureItem, $playerScore, $highestScoringEmg, $round, ReplacementType::HSP, $replaced);
					}
				}
				else if ($inDefence) {
					$kpdCount = $fixtureItem->getKppDefenderCount($homeOrAway);
					if ($kpdCount >= 2 && $kppEMG != null) {
						replacePlayer($fixtureItem, $playerScore, $kppEMG, $round, ReplacementType::KPP_Emergency, $replaced);
					}
					else if ($kpdCount == 1.5 && $halfKpdEMG != null && ($kpdEMG == null || $halfKpdEMG->score > $kpdEMG->score)) {
						replacePlayer($fixtureItem, $playerScore, $halfKpdEMG, $round, ReplacementType::Half_KPD_Emergency, $replaced);
					}
					else if ($kpdEMG != null) {
						replacePlayer($fixtureItem, $playerScore, $kpdEMG, $round, ReplacementType::KPD_Emergency, $replaced);
					}					
					else if ($fixtureItem->haveKPPRuckDefenderInRuck($homeOrAway) && $ruckEMG != null) {
						replacePlayer($fixtureItem, $playerScore, $ruckEMG, $round, ReplacementType::Ruck_Emergency_With_KPP_Ruck_Slide_In_Defence, $replaced);
					}
				}
				else if ($kppCount == 3.5 && $halfKppEMG != null && ($kppEMG == null || $halfKppEMG->score > $kppEMG->score)) {
					replacePlayer($fixtureItem, $playerScore, $halfKppEMG, $round, ReplacementType::Half_KPP_Emergency, $replaced);
				}
				else if ($kppEMG != null) {
					replacePlayer($fixtureItem, $playerScore, $kppEMG, $round, ReplacementType::KPP_Emergency, $replaced);
				}
				else if ($fixtureItem->haveKPPRuckInRuck($homeOrAway) && $ruckEMG != null) {
					replacePlayer($fixtureItem, $playerScore, $ruckEMG, $round, ReplacementType::Ruck_Emergency_With_KPP_Ruck_Slide, $replaced);
				}
			}
		}
		
		if ($replaced == 3) {
			break;
		}
	}
}

function replacePlayer(&$fixtureItem, &$playerToReplace, $playerToReplaceWith, $round, $replaceDescription, &$replaced) {
	replaceZeroScoringPlayer($playerToReplace->playerId, $playerToReplaceWith->playerId, $round, $replaceDescription);
	$replaced++;
	$playerToReplace->replacementPlayer = $playerToReplaceWith;
	$fixtureItem->usedEmergencies[] = $playerToReplaceWith;
}

?>
